﻿using System;
using System.Data;
using System.Windows.Forms;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms.InternalSetting
{
    public partial class FmUserSetting : Form
    {
        public FmUserSetting()
        {
            InitializeComponent();
            FmLoad();
        }
        enum AddType
        {
            root = 1,
            child = 2,
            sameNode = 4,
            delete = 8,
            modify = 16,
        }
        private Common.SqlConnectionHelper SqlConnectionHelper = new Common.SqlConnectionHelper(Program.configs,AppSet.ConfigCode);
        private void FmLoad()
        {
            this.label1.Text = "*最多只能设置4级内容\n\n*每级最多00--99条";
            RefreshTreeView(null);
            this.treeView1.ExpandAll();
            this.treeView1.BackColor = Common.MyColor.NewColor_Lavender;
        }

        private void 同级增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeContrl(AddType.sameNode);
        }

        private void 增加子节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeContrl(AddType.child);
        }

        private void 删除节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeContrl(AddType.delete);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeContrl(AddType.modify);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeContrl(AddType.delete);
        }
        private void TreeNodeContrl(AddType types)
        {
            TreeNode treeNode = new TreeNode();
            //要添加的节点名称为空，即文本框是否为空
            if (string.IsNullOrEmpty(this.fcSName.Text.Trim()) && types != AddType.modify && types != AddType.delete)
            {
                MessageBox.Show("要添加的节点名称不能为空！");
                return;
            }
            treeNode.Text = this.fcSName.Text.Trim();
            treeNode.ToolTipText = this.fcshuoming.Text.Trim();

            switch (types)
            {
                case AddType.root:
                    //添加根节点
                    treeNode.Tag = "";
                    treeNode.Name = string.Format("{0:D2}", SqlClassLibrary.SqlGetSet.NewSetingId("", SqlConnectionHelper.sqlConnection));
                    SetSetting(treeNode);
                    this.treeView1.Nodes.Add(treeNode);
                    this.fcSName.Text = "";
                    break;
                case AddType.child:
                    if (this.treeView1.SelectedNode == null)
                    {
                        MessageBox.Show("请选择要添加子节点的节点！");
                        return;
                    }
                    treeNode.Tag = this.treeView1.SelectedNode.Name;
                    treeNode.Name = this.treeView1.SelectedNode.Name + string.Format("{0:D2}", SqlClassLibrary.SqlGetSet.NewSetingId(treeNode.Tag.ToString(), SqlConnectionHelper.sqlConnection));
                    SetSetting(treeNode);

                    this.treeView1.SelectedNode.Nodes.Add(treeNode);
                    this.fcSName.Text = "";
                    this.treeView1.SelectedNode.Expand();
                    break;
                case AddType.sameNode:
                    if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Parent == null)
                    {
                        TreeNodeContrl(AddType.root);
                        return;
                    }
                    treeNode.Tag = this.treeView1.SelectedNode.Tag;
                    treeNode.Name = this.treeView1.SelectedNode.Tag.ToString() + string.Format("{0:D2}", SqlClassLibrary.SqlGetSet.NewSetingId(treeNode.Tag.ToString(), SqlConnectionHelper.sqlConnection));
                    SetSetting(treeNode);

                    this.treeView1.SelectedNode.Parent.Nodes.Add(treeNode);
                    this.fcSName.Text = "";
                    break;
                case AddType.delete:
                    if (this.treeView1.SelectedNode == null)
                    {
                        MessageBox.Show("请选择要删除的节点！");
                        return;
                    }
                    DeleteSetting(this.treeView1.SelectedNode);
                    this.treeView1.SelectedNode.Remove();
                    break;
                case AddType.modify:
                    if (this.treeView1.SelectedNode == null)
                    {
                        MessageBox.Show("请选择要修改的节点！");
                        return;
                    }
                    treeNode = this.treeView1.SelectedNode;
                    Setting setting = new Setting
                    {
                        SIds = treeNode.Name,
                        SName = treeNode.Text
                    };
                    DataTable dataTable = SqlClassLibrary.SqlGetSet.GetSettingDataTable(setting, SqlConnectionHelper.sqlConnection);
                    setting = DrawingImage.DataTableToObject(dataTable, setting);

                    FmUserSettingModify fmModifySetting = new FmUserSettingModify(setting);
                    fmModifySetting.ShowDialog();
                    this.treeView1.Nodes.Clear();
                    RefreshTreeView(null);
                    this.treeView1.ExpandAll();
                    break;
            }
        }
        private void DeleteSetting(TreeNode treeNode)
        {
            foreach (TreeNode item in treeNode.Nodes)
            {
                DeleteSetting(item);
            }
            treeNode.Tag = "delete";
            SetSetting(treeNode);
        }
        private void SetSetting(TreeNode treeNode)
        {
            Setting setting = new Setting
            {
                FId = treeNode.Tag.ToString(),
                SIds = treeNode.Name,
                SName = treeNode.Text,
                DefaultValue = this.fcDefaultValue.Text,
                NowValue = this.fcNowValue.Text,
                SLevel = this.fcSLevel.Text,
                shuoming = this.fcshuoming.Text
            };
            int rowcount = 0;
            if (treeNode.Tag.ToString() == "delete")
            {
                rowcount = SqlClassLibrary.SqlGetSet.DeleteSetting(setting, SqlConnectionHelper.sqlConnection);
            }
            else
            {
                rowcount = SqlClassLibrary.SqlGetSet.SetSetting(setting, SqlConnectionHelper.sqlConnection);
            }
        }
        private void RefreshTreeView(TreeNode tn)//刷新树
        {
            Setting setting = new Setting();
            if (tn != null)
            {
                setting.FId = tn.Name.ToString();
            }
            DataTable dt = SqlClassLibrary.SqlGetSet.GetSettingDataTable(setting, SqlConnectionHelper.sqlConnection);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TreeNode treeNode = new TreeNode
                    {
                        Text = dr["SName"].ToString(),
                        Name = dr["SIds"].ToString(),
                        Tag = dr["FId"].ToString(),
                        ToolTipText = dr["shuoming"].ToString()
                    };
                    if (tn == null)
                    {
                        this.treeView1.Nodes.Add(treeNode);
                    }
                    else
                    {
                        tn.Nodes.Add(treeNode);
                    }
                    RefreshTreeView(treeNode);
                }
            }
        }
    }
}
